package com.example.dukesbank.web;

import javax.el.ELResolver;
import javax.el.ELContext;
import javax.el.PropertyNotFoundException;
import java.beans.FeatureDescriptor;
import java.util.Iterator;
import java.util.Collections;

/**
 * An <code>ELResolver</code> implementation to detect instances of
 * <code>EnumManagedBean</code> and invoke the <code>getEnum()</code> method.
 */
public class EnumResolver extends ELResolver {
	@Override
	public Object getValue(ELContext elContext, Object base, Object property) {
		if (((base != null) && (property != null))
				&& base instanceof EnumManagedBean) {
			elContext.setPropertyResolved(true);

			return ((EnumManagedBean) base).getEnum(property.toString());
		}

		return null;
	}

	@Override
	public Class<?> getCommonPropertyType(ELContext elContext, Object base) {
		return EnumManagedBean.class;
	}

	@Override
	public Class<?> getType(ELContext elContext, Object base, Object property) {
		if (((base != null) && (property != null))
				&& base instanceof EnumManagedBean) {
			elContext.setPropertyResolved(true);
			throw new PropertyNotFoundException();
		}

		return null;
	}

	@Override
	public void setValue(ELContext elContext, Object base, Object property,
			Object value) {
		if (((base != null) && (property != null))
				&& base instanceof EnumManagedBean) {
			elContext.setPropertyResolved(true);
			throw new PropertyNotFoundException(); // readonly
		}
	}

	@Override
	public boolean isReadOnly(ELContext elContext, Object base, Object property) {
		if (((base != null) && (property != null))
				&& base instanceof EnumManagedBean) {
			elContext.setPropertyResolved(true);

			return true; // readonly
		}

		return false;
	}

	@Override
	public Iterator<FeatureDescriptor> getFeatureDescriptors(
			ELContext elContext, Object base) {
		return Collections.<FeatureDescriptor> emptyList().iterator();
	}
}
